package com.huluobo.a2208mdmdemo.demo

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

/**
 *  Created by LC on 2025/1/9.
 */
@Composable
fun demo06() {
    //导航控制器
    val navController = rememberNavController()

    //导航首页 startDestination代表首先显示 builder 声明所有需要导航的地址
    NavHost(navController = navController, startDestination = "main", builder = {
        //route 相当于地址
        composable("main") { mainPage(navController = navController) }
        //{myName} 相当于形参,作为参数可以进行页面传递
        composable("sec/{myName}") {
            //it.arguments 返回值是一个bundle,所有可以传递任何值
            val student = it.arguments?.getString("myName")
            secondPage(student!!)
        }
    })
}

@Composable
fun mainPage(navController: NavController) {
    Text(text = "主页面",
        Modifier
            .fillMaxSize()
            .background(Color.Yellow)
            .clickable {
                //通过地址进行跳转,同时通过/来传递实参
                val student = Student("lc", "描述", "图片")
                navController.navigate("sec/$student")
            })
}

@Composable
fun secondPage(student: String) {
    Text(
        text = "二页面${student}",
        Modifier
            .fillMaxSize()
            .background(Color.Green)
    )
}